
const app = getApp()

Page({
  data: {
    click_button: 0,
    phone: '',
    code: '',
    password: '',
    send: true,
    alreadySend: false,
    second: 60,
    checked: false
  },
  
  onShow:function(){
    var that=this;
    that.register();
  },

  checkedTap:function(e){
    this.setData({
      checked: !this.data.checked
    })
  },

  xieyi:function(e){
    wx.showToast({
      title: '《云数用户协议》',
      icon: 'none',
      duration: 2000,
    })
  },

  zhengce:function(e){
    wx.showToast({
      title: '《云数隐私政策》',
      icon: 'none',
      duration: 2000,
    })
  },

  submit_result:function(){
    var that = this;
    if (that.data.phone == "") {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 2000,
      })
    } else if (!(/^1(3|4|5|7|8|9)\d{9}$/.test(that.data.phone))) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000,
      })
    } else if (!(that.data.code)) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 2000,
      })
    } else if (!(that.data.password)) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: 2000,
      })
    } else if (that.data.checked == false) {
      wx.showToast({
        title: '请同意《云数用户协议》和《云数隐私政策》',
        icon: 'none',
        duration: 2000,
      })
    } else {
      this.setData({
        click_button: 1
      })
      wx.request({
        url: app.globalData.domain + 'Index/Login/',
        method: "POST",
        data: {
          phone: that.data.phone,
          code: that.data.code,
          password: that.data.password,
          openid: wx.getStorageSync('openid')
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function(res) {
          if (res.data.flag == 1) {
            that.setData({
              phone: '',
              code: '',
              password:'',
              click_button: 0,
              send: true,
              alreadySend: false,
              second: 60
            })
          } else if (res.data.flag == 2) {
            wx.showToast({
              title: '验证码不正确',
              icon: 'none',
              duration: 2000,
              success: function() {
                that.setData({
                  code: '',
                  click_button: 0
                })
              }
            })
          } else {
            wx.showToast({
              title: '提交失败',
              icon: 'none',
              duration: 2000,
              success: function() {
                that.setData({
                  phone: '',
                  code: '',
                  password:'',
                  click_button: 0,
                  send: true,
                  alreadySend: false,
                  second: 60
                })
                wx.switchTab({
                  url: '/pages/center/center',
                })
              }
            })
          }
        }
      })
    }
  },

  //获取openid
  register: function () {
    var that = this;
    wx.login({
      success: function (myres) {
        console.log(myres.code)
        wx.request({
          url: app.globalData.domain + 'Index/Register/',
          method: "POST",
          data: {
            code: myres.code,
          },
          header: {
            "Content-Type" : "application/x-www-form-urlencoded"
          },
          success: function (res) {
            wx.setStorageSync("openid", res.data.openid);
          }
        })
      }
    })
  },

  getPhone: function (e) {
    this.setData({
      phone: e.detail.value,
    })
  },
  getCode: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  getPassword: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  sendMsg: function (options) {
    var that=this;
    if(that.data.phone==""){
      wx.showToast({
        title: '请填写您的手机号码',
        icon: 'none',
        duration: 2000,
      })
    }else{
      if ((/^1(3|4|5|7|8|9)\d{9}$/.test(that.data.phone))) {
        that.setData({
          alreadySend: true,
          send: false
        })
        that.timer();
        wx.request({
          url: app.globalData.domain + 'Index/SendCode/',
          method: "POST",
          data: {
            phone: that.data.phone,
            openid: wx.getStorageSync('openid')
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            wx.showToast({
              title: '短信发送成功',
              icon: 'success',
              duration: 2000,
            })
          }
        })
      }else{
        wx.showToast({
          title: '请填写正确的手机号码',
          icon: 'error',
          duration: 2000,
        })
      }
    }
  },

  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          this.setData({
            second: this.data.second - 1
          })
          if (this.data.second <= 0) {
            this.setData({
              second: 60,
              alreadySend: false,
              send: true
            })
            resolve(setTimer)
          }
        }
        , 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },

  goLogin : function () {
    wx.navigateTo({
      url: '/pages/center/login',
    })
  }

})
